package free.emaranhado.casadolago;

import free.emaranhado.casadolago.adapter.CategoryAdapter;
import free.emaranhado.casadolago.adapter.EventsAdapter;
import free.emaranhado.casadolago.listener.XMLParserListener;
import free.emaranhado.casadolago.model.Category;
import free.emaranhado.casadolago.requests.ApiRequests;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.AdapterView.OnItemClickListener;

public class Home extends Activity implements XMLParserListener {
	private ApiRequests mRequests;
	private CategoryAdapter mCategoryAdapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mRequests = new ApiRequests(getApplicationContext());
        
        mCategoryAdapter = new CategoryAdapter(this);
        
        Gallery categoryGallery = (Gallery) findViewById(R.id.category_gallery);
        categoryGallery.setAdapter(mCategoryAdapter);

        categoryGallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//                Intent i = new Intent(getBaseContext(), DetailEvent.class);
//                startActivity(i);
                mRequests.requestEvents("1",Home.this);
            }
        });
        
        Gallery eventsGallery = (Gallery) findViewById(R.id.events_gallery);
        eventsGallery.setAdapter(new EventsAdapter(this));

        eventsGallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//                Intent i = new Intent(getBaseContext(), DetailEvent.class);
//                startActivity(i);
                mRequests.requestEvents("1",Home.this);

            }
        });
        
        mRequests.requestCategories(this);

        
        //mRequests.requestEvents(null);
        
        /*setListAdapter(new ArrayAdapter<String>(this, R.layout.category_item, CATEGORIES));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                Toast.LENGTH_SHORT).show();
            
            Intent i = new Intent(getBaseContext(), ListEvents.class);
            startActivity(i);
            
          }
        });*/


    }
	public void onRequestFinish() {
		// TODO Auto-generated method stub
		
	}
	public void onRequestFinish(Object data) {
		Log.i("HomeUpdate",":= "+data);
		if(data instanceof Category){
			final Category mCategory = (Category) data;
			runOnUiThread(new Runnable() {
			    public void run() {
			    	addCategory(mCategory);
			    }
			});

		}
		
	}
	
	protected void addCategory(Category mCategory) {
    	mCategoryAdapter.addCategory(mCategory);
		mCategoryAdapter.notifyDataSetChanged();
		
	}
	

}